package com.xzx.car_accessories.web;

import com.xzx.car_accessories.domain.CarAccessory;
import com.xzx.car_accessories.service.CarAccessoriesManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import com.alibaba.fastjson.JSON;

/**
 * @author: geeshaw
 * @date: 2021/7/11 17:33
 * @email: shaw@bupt.edu.cn
 * @desc: 汽车配件信息管理
 **/

@RestController
@RequestMapping(value = "/carinfo")     // 通过这里配置使下面的映射都在/car
public class CarAccessoriesManageController {

    // 创建线程安全的Map，模拟汽车配件信息的存储
    static Map<Long, CarAccessory> carAccessories = Collections.synchronizedMap(new HashMap<Long, CarAccessory>());

    @Autowired
    private CarAccessoriesManageService carAccessoriesManageService;
    /**
     * 处理"//"的GET请求，查询所有商品属性
     */
    @GetMapping("/search/findall")
    public List<CarAccessory> get_all_accessories() {
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        return carAccessoriesManageService.get_all_accessories();
    }

    /**
     * 处理"//"的GET请求，查询所有汽车配件属性的单个需求
     */
    @GetMapping("/search/{key}/{value}")
    @ApiOperation("模糊搜寻汽车配件属性信息")
    public List<CarAccessory> get_diff_accessory(@PathVariable String key, @PathVariable Object value) {
        Map<String, Object> query_map  = new HashMap<>();
        query_map.put(key, value);
        return carAccessoriesManageService.get_diff_accessory(query_map);
    }

    /**
     * 处理的POST请求，增加一个汽车配件信息
     */
    @PostMapping("/save")
    public String add_car_accssory(@RequestBody CarAccessory carAccessory) {
        System.out.println(carAccessory);
        // @RequestBody注解用来绑定通过http请求中application/json类型上传的数据
        int i = carAccessoriesManageService.add_accessory(new CarAccessory(
                carAccessory.getCid(),
                carAccessory.getCname(),
                carAccessory.getCategory(),
                carAccessory.getManufacturer()
                )
        );
        System.out.println("---------------");
        Map<String, Integer> res = new HashMap<String,Integer>();
        if(i>0){
            res.put("code", 200);
        }
        else{
            res.put("code", 500);
        }

        return JSON.toJSONString(res);
    }

    /**
     * 处理的POST请求，更新汽车配件信息
     */
    @PostMapping("/update")
    public String update_car_accssory(@RequestBody CarAccessory carAccessory) {
        // @RequestBody注解用来绑定通过http请求中application/json类型上传的数据
        int i = carAccessoriesManageService.update_accessory(new CarAccessory(
                        carAccessory.getCid(),
                        carAccessory.getCname(),
                        carAccessory.getCategory(),
                        carAccessory.getManufacturer()
                )
        );
        Map<String, Integer> res = new HashMap<String,Integer>();
        if(i>0){
            res.put("code", 200);
        }
        else{
            res.put("code", 500);
        }

        return JSON.toJSONString(res);
    }

    /**
     * 处理的 GET 请求，查询汽车配件的分页信息
     */
    @GetMapping("/findPage")
    @ApiOperation("查询汽车配件的分页信息")
    public String get_carinfo_page(String pageNum, String pageSize, String searchWord) {
        System.out.println(searchWord);
        System.out.println(pageNum);
        System.out.println(pageSize);
        List<CarAccessory> contentTotal = carAccessoriesManageService.search_accessory(searchWord);
        List<CarAccessory> content = carAccessoriesManageService.search_findpage_accessory(Integer.parseInt(pageNum),
                Integer.parseInt(pageSize), searchWord);
        Map<String, Object> findPageData = new HashMap<String, Object>();
        Map<String, Object> data = new HashMap<String, Object>();
        int totalSize = contentTotal.size();
        int totalPages  ;
        if(totalSize % Integer.parseInt(pageSize) == 0)
        {
            totalPages =  totalSize / Integer.parseInt(pageSize);
        }
        else
        {
            totalPages =  totalSize / Integer.parseInt(pageSize) + 1;
        }

        data.put("pageNum", pageNum);
        data.put("pageSize", pageSize);
        data.put("totalSize", totalSize);
        data.put("totalPages", totalPages);
        data.put("content", content);
        findPageData.put("msg", null);
        findPageData.put("code", 200);
        findPageData.put("data", data);

        String param = JSON.toJSONString(findPageData);
        System.out.println(param);
        // 封装成功
        return param;
    }

    /**
     * 处理的 GET 请求，查询汽车配件的分页信息
     */
    @GetMapping("/search")
    @ApiOperation("模糊查询汽车配件")
    public String search_carinfo(String pageNum, String pageSize, String searchWord) {

        List<CarAccessory> contentTotal = carAccessoriesManageService.search_accessory(searchWord);
        List<CarAccessory> content = carAccessoriesManageService.search_findpage_accessory(Integer.parseInt(pageNum),
                Integer.parseInt(pageSize), searchWord);

        Map<String, Object> findPageData = new HashMap<String, Object>();
        Map<String, Object> data = new HashMap<String, Object>();

        int totalSize = contentTotal.size();
        int totalPages;

        if(totalSize % Integer.parseInt(pageSize) == 0)
        {
            totalPages =  totalSize / Integer.parseInt(pageSize);
        }
        else
        {
            totalPages =  totalSize / Integer.parseInt(pageSize) + 1;
        }

        data.put("pageNum", pageNum);
        data.put("pageSize", pageSize);
        data.put("totalSize", totalSize);
        data.put("totalPages", totalPages);
        data.put("content", content);
        findPageData.put("msg", null);
        findPageData.put("code", 200);
        findPageData.put("data", data);

        String param = JSON.toJSONString(findPageData);
        // 封装成功
        System.out.println(param);
        return param;
    }

    @PostMapping("/del")
    public String del_car_accssory(@RequestBody List<Map<String, String>> idList) {
        System.out.println(idList);
        Map<String, Integer> res = new HashMap<String,Integer>();
        Boolean isSuccess = false;
        for (int i = 0; i < idList.size(); i++) {
            int j = carAccessoriesManageService.del_accessory(idList.get(i).get("id"));
            if(j>0){
                isSuccess = true;
            }
        }
        if(isSuccess){
            res.put("code", 200);
        }
        else{
            res.put("code", 500);
        }
        return JSON.toJSONString(res);
    }

    /**
     * 处理的 GET 请求，查询汽车配件的分页信息
     */
    @GetMapping("/group/category")
    @ApiOperation("对汽车信息进行分类")
    public String group_category() {
        return JSON.toJSONString(carAccessoriesManageService.groupCategory());
    }

    /**
     * 处理的 GET 请求，查询汽车配件的分页信息
     */
    @GetMapping("/group/manufacturer")
    @ApiOperation("对汽车信息进行分类")
    public String group_manufacturer() {
        return JSON.toJSONString(carAccessoriesManageService.groupManufacturer());
    }

    /**
     * 处理"/users/{id}"的DELETE请求，用来删除User
     */
//    @DeleteMapping("/del/{key}/{value}")
//    public String delete_accessory(@PathVariable String key, @PathVariable Object value) {
//        Map<String, Object> query_map  = new HashMap<>();
//        query_map.put(key, value);
//        return Integer.toString(carAccessoriesManageService.del_accessory(query_map));
//    }

}
